﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Web.Script.Services;
using System.Data;
using System.Collections;
using CE.Database;

/// <summary>
/// Summary description for get_car_details
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class get_car_details : System.Web.Services.WebService {

    public get_car_details () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCarMakeList(string Year, string CarMake, string CarModel)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblMake");
        SortedList _spList = new SortedList();
        _spList.Add("@Year", Year);
        _dt = _db.GetDataTableBySP("sprocGetCarMakeTHListByYear", _spList);

        string[] _strQuoteCount = GetCarQuoteCountLast30Days(CarMake, CarModel);
        //string[] _strQuoteCount = new string[2];
        //_strQuoteCount[0] = "123";
        //_strQuoteCount[1] = "456";

        StringWriter builder = new StringWriter();

        builder.WriteLine("[");
        if (_dt.Rows.Count > 0)
        {
            //builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            //builder.WriteLine("\"optionValue\":\"-1\"},");
            for (int i = 0; i <= _dt.Rows.Count - 1; i++)
            {
                //builder.WriteLine("{\"optionDisplay\":\"" + _dt.Rows[i][1] + "\",");
                //builder.WriteLine("\"optionValue\":\"" + _dt.Rows[i][0] + "\"},");
                builder.WriteLine("{\"optionDisplay\":\"" + _dt.Rows[i][1] + "\",");
                builder.WriteLine("\"optionValue\":\"" + _dt.Rows[i][0] + "\",");
                builder.WriteLine("\"quoteCount\":\"" + _strQuoteCount[0] + "\",");
                builder.WriteLine("\"carBrand\":\"" + _strQuoteCount[1] + "\"},");
            }
        }
        else
        {
            //builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            //builder.WriteLine("\"optionValue\":\"-1\"},");
        }
        string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
        returnjson = returnjson + "]";
        return returnjson.Replace("\r", "").Replace("\n", "");
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCarModelDescList(string Year, string CarMake, string CarModel)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblModelDesc");
        SortedList _spList = new SortedList();
        _spList.Add("@CarMake", CarMake);
        _spList.Add("@Year", Year);
        _dt = _db.GetDataTableBySP("sprocGetCarModelDescTHListByCarMakeCode", _spList);

        string[] _strQuoteCount = GetCarQuoteCountLast30Days(CarMake, CarModel);

        StringWriter builder = new StringWriter();

        builder.WriteLine("[");
        if (_dt.Rows.Count > 0)
        {
            //builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            //builder.WriteLine("\"optionValue\":\"-1\"},");
            for (int i = 0; i <= _dt.Rows.Count - 1; i++)
            {
                //builder.WriteLine("{\"optionDisplay\":\"" + _dt.Rows[i]["CarModelDisplay"] + "\",");
                //builder.WriteLine("{\"optionValue\":\"" + _dt.Rows[i]["CarModelDesc"] + "\"},");
                builder.WriteLine("{\"optionDisplay\":\"" + _dt.Rows[i]["CarModelDisplay"] + "\",");
                builder.WriteLine("\"optionValue\":\"" + _dt.Rows[i]["CarModelDesc"] + "\",");
                builder.WriteLine("\"quoteCount\":\"" + _strQuoteCount[0] + "\",");
                builder.WriteLine("\"carBrand\":\"" + _strQuoteCount[1] + "\"},");
            }
        }
        else
        {
            //builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            //builder.WriteLine("\"optionValue\":\"-1\"},");
        }
        string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
        returnjson = returnjson + "]";
        return returnjson.Replace("\r", "").Replace("\n", "");
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCarQuoteCountLast30DaysByCarModel(string CarMake, string CarModel)
    {
        string returnjson = "";
        try
        {
            string[] _tempDesc = CarModel.Split(' ');
            string _model = "";
            string _body = "";
            string _cc = "";
            string _door = "";
            if (_tempDesc.Length == 4)
            {
                _model = _tempDesc[0];
                _body = "-";// _tempDesc[2] == "2" ? "PICKUP" : "";
                _cc = _tempDesc[1].Substring(0, _tempDesc[1].Length - 2);
                _door = _tempDesc[2];
            }
            else if (_tempDesc.Length == 5)
            {
                _model = _tempDesc[0];
                _body = _tempDesc[1];
                _cc = _tempDesc[2].Substring(0, _tempDesc[2].Length - 2);
                _door = _tempDesc[3];
            }
            else if (_tempDesc.Length == 6)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1];
                _body = _tempDesc[2];
                _cc = _tempDesc[3].Substring(0, _tempDesc[3].Length - 2);
                _door = _tempDesc[4];
            }
            else if (_tempDesc.Length == 7)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1] + " " + _tempDesc[2];
                _body = _tempDesc[3];
                _cc = _tempDesc[4].Substring(0, _tempDesc[4].Length - 2);
                _door = _tempDesc[5];
            }

            string _strQuoteCount = "", _strCarBrand = "";
            Int32 _TotalCount = 0;
            Int32 _CarMakeCount = 0;
            Int32 _CarModelCount = 0;
            MyDatabase _db = new MyDatabase();
            DataTable _dtCarQuoteCount = new DataTable("tblCarQuoteCount");
            SortedList _spList = new SortedList();
            _spList.Add("@CarMake", CarMake);
            _spList.Add("@CarModel", _model);
            _dtCarQuoteCount = _db.GetDataTableBySP("sprocGGCarQuoteCountLast30DaysSelectedItem", _spList);
            if (_dtCarQuoteCount != null && _dtCarQuoteCount.Rows.Count > 0)
            {
                if (!Int32.TryParse(_dtCarQuoteCount.Rows[0]["TotalCount"].ToString(), out _TotalCount))
                {
                    _TotalCount = 0;
                }
                if (!Int32.TryParse(_dtCarQuoteCount.Rows[0]["CarMakeCount"].ToString(), out _CarMakeCount))
                {
                    _CarMakeCount = 0;
                }
                if (!Int32.TryParse(_dtCarQuoteCount.Rows[0]["CarModelCount"].ToString(), out _CarModelCount))
                {
                    _CarModelCount = 0;
                }
            }
            if (_CarModelCount > 0)
            {
                _strQuoteCount = _CarModelCount.ToString();
                _strCarBrand = GetMotorBrand(CarMake);
            }
            else if (_CarMakeCount > 0)
            {
                _strQuoteCount = _CarMakeCount.ToString();
                _strCarBrand = GetMotorBrand(CarMake);
            }
            else if (_TotalCount > 0)
            {
                _strQuoteCount = _TotalCount.ToString();
                _strCarBrand = GetMotorBrand("GG");
            }
            _dtCarQuoteCount = null;
            _db = null;

            StringWriter builder = new StringWriter();

            builder.WriteLine("[");
            builder.WriteLine("{\"quoteCount\":\"" + _strQuoteCount + "\",");
            builder.WriteLine("\"carBrand\":\"" + _strCarBrand + "\"},");
            returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
            returnjson = returnjson + "]";
            returnjson = returnjson.Replace("\r", "").Replace("\n", "");
        }
        catch (Exception ex)
        {
        }
        return returnjson;
    }

    private string[] GetCarQuoteCountLast30Days(string CarMake, string CarModel)
    {
        string[] _strReturn = new string[2];
        try
        {
            string _strQuoteCount = "", _strCarBrand = "";
            Int32 _TotalCount = 0;
            Int32 _CarMakeCount = 0;
            Int32 _CarModelCount = 0;
            MyDatabase _db = new MyDatabase();
            DataTable _dtCarQuoteCount = new DataTable("tblCarQuoteCount");
            SortedList _spList = new SortedList();
            _spList.Add("@CarMake", CarMake);
            _spList.Add("@CarModel", CarModel);
            _dtCarQuoteCount = _db.GetDataTableBySP("sprocGGCarQuoteCountLast30DaysSelectedItem", _spList);
            if (_dtCarQuoteCount != null && _dtCarQuoteCount.Rows.Count > 0)
            {
                if (!Int32.TryParse(_dtCarQuoteCount.Rows[0]["TotalCount"].ToString(), out _TotalCount))
                {
                    _TotalCount = 0;
                }
                if (!Int32.TryParse(_dtCarQuoteCount.Rows[0]["CarMakeCount"].ToString(), out _CarMakeCount))
                {
                    _CarMakeCount = 0;
                }
                if (!Int32.TryParse(_dtCarQuoteCount.Rows[0]["CarModelCount"].ToString(), out _CarModelCount))
                {
                    _CarModelCount = 0;
                }
            }
            if (_CarModelCount > 0)
            {
                _strQuoteCount = _CarModelCount.ToString();
                _strCarBrand = GetMotorBrand(CarMake);
            }
            else if (_CarMakeCount > 0)
            {
                _strQuoteCount = _CarMakeCount.ToString();
                _strCarBrand = GetMotorBrand(CarMake);
            }
            else if (_TotalCount > 0)
            {
                _strQuoteCount = _TotalCount.ToString();
                _strCarBrand = GetMotorBrand("GG");
            }
            _dtCarQuoteCount = null;
            _db = null;

            _strReturn[0] = _strQuoteCount;
            _strReturn[1] = _strCarBrand;
        }
        catch (Exception ex)
        {
        }
        return _strReturn;
    }

    public string GetMotorBrand(string Brand)
    {
        string _strBrandClass = "gluaygluay";
        if (Brand == "AUDI")
        {
            _strBrandClass = "audi";
        }
        else if (Brand == "BMW")
        {
            _strBrandClass = "bmw";
        }
        else if (Brand == "CHER")
        {
            _strBrandClass = "chery";
        }
        else if (Brand == "CHEV")
        {
            _strBrandClass = "chevrolet";
        }
        else if (Brand == "CITR")
        {
            _strBrandClass = "citroen";
        }
        else if (Brand == "DAIH")
        {
            _strBrandClass = "daihatsu";
        }
        else if (Brand == "DFM")
        {
            _strBrandClass = "dfm";
        }
        else if (Brand == "FORD")
        {
            _strBrandClass = "ford";
        }
        else if (Brand == "HOND")
        {
            _strBrandClass = "honda";
        }
        else if (Brand == "HYUN")
        {
            _strBrandClass = "hyundai";
        }
        else if (Brand == "ISUZ")
        {
            _strBrandClass = "isuzu";
        }
        else if (Brand == "JAGU")
        {
            _strBrandClass = "jaguar";
        }
        else if (Brand == "JEEP")
        {
            _strBrandClass = "jeep";
        }
        else if (Brand == "KIA")
        {
            _strBrandClass = "kia";
        }
        else if (Brand == "LAND")
        {
            _strBrandClass = "landrover";
        }
        else if (Brand == "LEXU")
        {
            _strBrandClass = "lexus";
        }
        else if (Brand == "MAZD")
        {
            _strBrandClass = "mazda";
        }
        else if (Brand == "MERC")
        {
            _strBrandClass = "mercedes";
        }
        else if (Brand == "MINI")
        {
            _strBrandClass = "mini";
        }
        else if (Brand == "MITS")
        {
            _strBrandClass = "mitsubishi";
        }
        else if (Brand == "NISS")
        {
            _strBrandClass = "nissan";
        }
        else if (Brand == "PEUG")
        {
            _strBrandClass = "peugeot";
        }
        else if (Brand == "PORS")
        {
            _strBrandClass = "porsche";
        }
        else if (Brand == "PROT")
        {
            _strBrandClass = "proton";
        }
        else if (Brand == "SSAN")
        {
            _strBrandClass = "ssangyong";
        }
        else if (Brand == "SUBA")
        {
            _strBrandClass = "subaru";
        }
        else if (Brand == "SUZU")
        {
            _strBrandClass = "suzuki";
        }
        else if (Brand == "TATA")
        {
            _strBrandClass = "tata";
        }
        else if (Brand == "TOYO")
        {
            _strBrandClass = "toyota";
        }
        else if (Brand == "VOLK")
        {
            _strBrandClass = "volkswagen";
        }
        else if (Brand == "VOLV")
        {
            _strBrandClass = "volvo";
        }
        return _strBrandClass;
    }
    
}

